home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / TCP_IP / TNOS230S / DUMBCONS.C < prev    next >
C/C++ Source or Header  |  1997-06-28  |  5KB  |  254 lines

  1. /*
  2.  * A "dumb console" session manager.  This is intended for use when stdin/out
  3.  * aren't ttys.  But stdin must still be usable for input.
  4.  */
  5.  
  6. #include "global.h"
  7. #ifdef SM_DUMB
  8. #include <termios.h>
  9. #include "hardware.h"
  10. #include "proc.h"
  11. #include "tty.h"
  12. #include "sessmgr.h"
  13.  
  14. #if !defined(_lint)
  15. static char rcsid[] OPTIONAL = "$Id: dumbcons.c,v 1.20 1997/06/28 16:46:13 root Exp root $";
  16. #endif
  17.  
  18.  
  19. extern int Keyboard;
  20. extern int Numrows, Numcols;
  21.  
  22.  
  23. static struct termios old_tty, new_tty;
  24. static int Suspense = 0, ttyed;
  25.  
  26. static int
  27. dumb_init(const struct sessmgr_sw *sm OPTIONAL)
  28. {
  29.     ttyed = (tcgetattr(0, &old_tty) != -1);
  30.     if (ttyed)    {
  31.     new_tty = old_tty;
  32.     new_tty.c_lflag &= ~(ISIG|ICANON|ECHO|ECHOE|ECHOK|ECHONL);
  33.     new_tty.c_cc[VMIN] = 1;
  34.     new_tty.c_cc[VTIME] = 0;
  35.     (void) tcsetattr(0, TCSADRAIN, &new_tty);
  36.     }
  37.     /* suppress flow control */
  38.     Numrows = 0;
  39.     Numcols = 0;
  40.     return 1;
  41. }
  42.  
  43. static void
  44. dumb_end(const struct sessmgr_sw *sm OPTIONAL)
  45. {
  46.     if (ttyed && !Suspense)
  47.     (void) tcsetattr(0, TCSADRAIN, &old_tty);
  48. }
  49.  
  50. static void
  51. dumb_suspend(const struct sessmgr_sw *sm OPTIONAL)
  52. {
  53.     if (!Suspense++)
  54.     {
  55.     (void) fflush(stdout);
  56.     if (ttyed)
  57.         (void) tcsetattr(0, TCSADRAIN, &old_tty);
  58.     }
  59. }
  60.  
  61. static void
  62. dumb_resume(const struct sessmgr_sw *sm OPTIONAL)
  63. {
  64.     if (!--Suspense)
  65.     {
  66.     if (ttyed)
  67.         (void) tcsetattr(0, TCSADRAIN, &new_tty);
  68.     Numrows = 0;
  69.     Numcols = 0;
  70.     }
  71. }
  72.  
  73. static int
  74. dumb_swap(const struct sessmgr_sw *sm OPTIONAL, void *old, void *new)
  75. {
  76.     int c;
  77.  
  78.     if (old && new)
  79.     {
  80.     c = (int) new - 1;
  81.     printf("\n>>> switching to session %d", c);
  82.     if (Sessions[c].name && *Sessions[c].name)
  83.         printf(": %s", Sessions[c].name);
  84.     else if (Sessions[c].proc->name && *Sessions[c].proc->name)
  85.         printf(" [%s]", Sessions[c].proc->name);
  86.     printf(" (%s)\n", Sestypes[Sessions[c].type]);
  87.     }
  88.     return 0;            /* only the current session can do output */
  89. }
  90.  
  91. static void
  92. dumb_putch(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL, int c)
  93. {
  94.     putchar(c);        /*lint !e415 !e534 */
  95. }
  96.  
  97. static void
  98. dumb_clreol(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL)
  99. {
  100. }
  101.  
  102. static void
  103. dumb_rflush(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL)
  104. {
  105. }
  106.  
  107. static void
  108. dumb_flush(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL)
  109. {
  110.     if (!Suspense)
  111.     (void) fflush(stdout);
  112. }
  113.  
  114. static void *
  115. dumb_create(const struct sessmgr_sw *sm OPTIONAL, struct session *sp)
  116. {
  117.     return (void *) (sp - Sessions + 1);
  118. }
  119.  
  120. static void
  121. dumb_destroy(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL)
  122. {
  123. }
  124.  
  125. static void
  126. dumb_clrscr(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL)
  127. {
  128. }
  129.  
  130. static int
  131. dumb_wherex(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL)
  132. {
  133.     return -1;
  134. }
  135.  
  136. static int
  137. dumb_wherey(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL)
  138. {
  139.     return -1;
  140. }
  141.  
  142. static void
  143. dumb_window(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL,
  144.     int x1 OPTIONAL, int y1 OPTIONAL, int x2 OPTIONAL, int y2 OPTIONAL)
  145. {
  146. }
  147.  
  148. static void
  149. dumb_gotoxy(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL,
  150.     int x OPTIONAL, int y OPTIONAL)
  151. {
  152. }
  153.  
  154. static void
  155. dumb_high(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL)
  156. {
  157. }
  158.  
  159. static void
  160. dumb_norm(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL)
  161. {
  162. }
  163.  
  164. static void
  165. dumb_bground(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL, int c OPTIONAL)
  166. {
  167. }
  168.  
  169. static void
  170. dumb_fground(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL, int c OPTIONAL)
  171. {
  172. }
  173.  
  174. static void
  175. dumb_txtattr(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL, int c OPTIONAL)
  176. {
  177. }
  178.  
  179. static void
  180. dumb_refresh (const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL)
  181. {
  182. }
  183.  
  184. static void
  185. dumb_cursor(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL, int c OPTIONAL)
  186. {
  187. }
  188.  
  189. /*
  190.  * We define two special characters:  ^C is F10, ^T is F9.  Everything else is
  191.  * passed literally.
  192.  */
  193.  
  194. static int
  195. dumb_kbread(const struct sessmgr_sw *sm OPTIONAL, void *dp OPTIONAL)
  196. {
  197.     unsigned char ch;
  198.     int i;
  199.  
  200.     do
  201.     {
  202.     kwait(&Keyboard);
  203.     }
  204.     while ((i = read(0, &ch, 1)) == 0 || (i == -1 && errno == EWOULDBLOCK));
  205.     if (i < 0)
  206.     {
  207.     tputs("NOS PANIC: Lost keyboard\n");
  208.     where_outta_here(1, "dumb_kbread");
  209.     }
  210.     if ((i = ch) == 3)
  211.     i = -2;
  212.     else if (i == 20)
  213.     i = -11;
  214.     else if (i == 127)        /* for when the backspace key sends DEL */
  215.     i = 8;
  216.     return i;
  217. }
  218.  
  219. struct sessmgr_sw dumb_sessmgr =
  220. {
  221.     "dumb",
  222.     SM_STDIO,
  223.     dumb_init,
  224.     (char *(*)(const struct sessmgr_sw *, char *)) 0,
  225.     dumb_create,
  226.     (char *(*)(const struct sessmgr_sw *, void *, const char *)) 0,
  227.     dumb_swap,
  228.     dumb_putch,
  229.     dumb_clreol,
  230.     dumb_clrscr,
  231.     dumb_wherex,
  232.     dumb_wherey,
  233.     dumb_window,
  234.     dumb_gotoxy,
  235.     dumb_high,
  236.     dumb_norm,
  237.     dumb_bground,
  238.     dumb_fground,
  239.     dumb_txtattr,
  240.     dumb_refresh,
  241.     dumb_cursor,
  242.     dumb_kbread,
  243.     dumb_destroy,
  244.     0,
  245.     dumb_rflush,
  246.     dumb_flush,
  247.     dumb_suspend,
  248.     dumb_resume,
  249.     dumb_end,
  250.     0
  251. };
  252.  
  253. #endif
  254.